package com.kmzx.gen.signs.api.impl;

import com.kmzx.gen.signs.api.DataApi;
import com.kmzx.gen.signs.config.Constants;
import com.kmzx.gen.signs.dao.GenDataMapper;
import com.kmzx.gen.signs.dto.KVData;
import com.kmzx.gen.signs.entity.GenData;
import com.kmzx.gen.signs.utils.UUIDGenerator;
import com.kmzx.gen.signs.web.ReceiptDataController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Project gen_sign
 * @Author: carol
 * @Date: 2020/7/7-14:54
 */
@Component
public class DataApiImpl implements DataApi {

    private static Logger logger = LoggerFactory.getLogger(DataApiImpl.class);


    @Autowired
    GenDataMapper genDataMapper;

    @Autowired
    private RedisTemplate<String, Object> stringObjectRedisTemplate;

    @Override
    public List<GenData> findLatestGenData() {
        List<GenData> data = genDataMapper.selectDataList();
        for (GenData datum : data) {
            String fromData = datum.getFromData();
            fromData = fromData.replaceAll("\"","\\\"");
            datum.setFromData(fromData);
        }
        return data;
    }

    @Override
    public int batchUse(List<GenData> list) {
        if (!CollectionUtils.isEmpty(list)) {

            for (GenData genData : list) {
                genData.setUseful(1).setUpdateTime(new Date());
            }
            genDataMapper.batchUsefulData(list);
        }
        return 0;
    }

    @Override
    public int saveGenDataOrder(KVData kvData) {
        GenData data = new GenData();
        Date date = new Date();
        data.setId(UUIDGenerator.generate())
                .setFinished(0)
                .setSend(0).setSuccess(0).setSessionId(kvData.getSid())
                .setCreateTime(date).setUpdateTime(date)
                .setFromData(kvData.getFromData()).setUseful(0)
                .setOrderCode(kvData.getOrderCode()).setDataId(kvData.getDataId());
        genDataMapper.insertGenData(data);
        return 0;
    }

    @Override
    public int batchSaveGenDataOrder(List<KVData> datas) {
        ArrayList<GenData> list = new ArrayList<>();
        for (KVData kvData : datas) {
            GenData data = new GenData();
            Date date = new Date();
            data.setId(UUIDGenerator.generate())
                    .setFinished(0)
                    .setSend(0).setSuccess(0).setSessionId(kvData.getSid())
                    .setFromData(kvData.getFromData())
                    .setCreateTime(date).setUpdateTime(date)
                    .setOrderCode(kvData.getOrderCode()).setDataId(kvData.getDataId());
            list.add(data);
        }

        genDataMapper.batchInsertGenData(list);
        return 0;
    }

    @Override
    public int batchUpdateData(List<GenData> datas) {
        Date date = new Date();
        for (GenData kvData : datas) {
            Integer ok = kvData.getOk();
            if (ok == 1) {
                kvData.setSuccess(1).setUpdateTime(date);
            }
        }
        int update = genDataMapper.batchUpdateByIdSelective(datas);
        return update;
    }

    @Override
    public int updateSignedData(String id, String signData) {
        GenData data = new GenData();
        data.setId(id).setUpdateTime(new Date())
                .setSignedData(signData)
                .setSuccess(1);

        stringObjectRedisTemplate.opsForValue().set(Constants.sea_upload_Prefix+id, id, 1, TimeUnit.SECONDS);
        int update = genDataMapper.updateByIdSelective(data);
        return update;
    }


    @Override
    public int errorSignData(String id,String reason){
        GenData data = new GenData();
        data.setId(id).setUpdateTime(new Date())
                .setReason(reason)
                .setSuccess(2);
        return 0;
    }

    @Override
    public int batchFinish(List<KVData> list) {
        Date date = new Date();
        ArrayList<GenData> end = new ArrayList<>();
        for (KVData kvData : list) {
            String id = kvData.getId();
            GenData genData = new GenData();
            genData.setId(id).setFinished(1).setSuccess(kvData.getOk());
            genData.setUpdateTime(date);
            end.add(genData);
        }
        int finish = genDataMapper.batchSendFinish(end);
        return finish;
    }


}
